package com.csu.petstore.controller;

import com.csu.petstore.common.CommonResponse;
import com.csu.petstore.log.Syslog;
import com.csu.petstore.pojo.Account;
import com.csu.petstore.pojo.Profile;
import com.csu.petstore.pojo.User;
import com.csu.petstore.service.AccountAndProfileService;
import com.csu.petstore.service.UserService;

import com.csu.petstore.utils.jwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AccountAndProfileService accountAndProfileService;

    //验证用户
    @PostMapping("/login")
    @Syslog(modelName = "请求登录",methodDesc = "检查登录",methodType = 2)
    public CommonResponse<String> CheckUser(@RequestParam("username") String username,
                            @RequestParam("password") String password,HttpServletRequest request){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        String logMsg = userService.isRightOfUser(username, password);
        System.out.println(logMsg);
        //登陆成功操作
        if (logMsg.equals("success")){
            HttpSession session = request.getSession();
            Account account = accountAndProfileService.getAccountByUserId(username);
            Profile profile = accountAndProfileService.getProfileBYUserId(username);
            System.out.println(account);
            System.out.println(profile);
            String token = jwtUtils.getToken(username);
            System.out.println(token);
            session.setAttribute("token",token);
            //把这些信息放到session里面使进入myaccount的时候可以用
            session.setAttribute("username",username);
            session.setAttribute("account",account);
            session.setAttribute("profile",profile);
            return CommonResponse.createForSuccess();
        }
        return CommonResponse.createForError();

    }
    //在使用对象进行绑定表单属性的时候需要先把对象加入Model才可以进行获取对象
    //验证注册信息是否有误
    @Syslog(modelName = "请求注册",methodDesc = "检查注册信息",methodType = 2)
    @PostMapping("/register")
    public CommonResponse<String> CheckRegister(@Validated Account account, BindingResult bindingResult, Profile profile){
        //设置参数异常处理
        System.out.println(account);
        System.out.println(profile);
        String errorMsg = accountAndProfileService.ExceptionHandlerOfAccount(bindingResult,account);
        System.out.println(errorMsg);
        if (errorMsg.isEmpty()){
            accountAndProfileService.setAccountAndProfileMsg(account,profile);
            return CommonResponse.createForSuccess();
        }else {
            return CommonResponse.createForError();
        }
    }
    //更新Account信息
    @Syslog(modelName = "个人信息",methodDesc = "更新个人信息",methodType = 2)
    @PostMapping("updateaccount")
    public CommonResponse<String> UpdateAccount(HttpServletRequest request,@Validated Account account, BindingResult bindingResult,Profile profile){
        HttpSession session = request.getSession();
        String OldUserId = (String) session.getAttribute("username");
        System.out.println(OldUserId);
        System.out.println(account);
        System.out.println(profile);
        //与检查注册一样的操作进行参数异常处理
        String errorMsg = accountAndProfileService.ExceptionHandlerOfAccount(bindingResult,account);
        if (errorMsg.isEmpty()){
            //删除旧的账户信息
            accountAndProfileService.deleteByUserId(OldUserId);
            //把新的账户信息加入数据库
            accountAndProfileService.setAccountAndProfileMsg(account,profile);
            //更新signon表内的信息
            User user = new User(account.getUserid(),account.getPassword());
            userService.updateUserByUserName(user,OldUserId);
            //更新session里面的账户信息
            session.setAttribute("username",account.getUserid());
            session.setAttribute("account",account);
            session.setAttribute("profile",profile);
            return CommonResponse.createForSuccess();
        }
        else {
            return CommonResponse.createForError();
        }
    }

    @PostMapping("checkRegister")
    @ResponseBody
    public CommonResponse<String> RegisterCheck(@Validated @ModelAttribute Account account, BindingResult bindingResult, @ModelAttribute Profile profile,Model model){
        System.out.println(account);
        System.out.println(profile);
        //设置参数异常处理
        String errorMsg = accountAndProfileService.ExceptionHandlerOfAccount(bindingResult,account);
        if (errorMsg.isEmpty()){
            accountAndProfileService.setAccountAndProfileMsg(account,profile);
           return CommonResponse.createForSuccessMessage("success");
        }else {
            return CommonResponse.createForSuccessMessage(errorMsg);
        }
    }
}
